package bcxd.firstline.focalcompany.machining;

import java.sql.Connection;
import java.util.Map;

import jt.classic.system.user.IUser;

import org.limp.basework.MachiningException;
import org.limp.basework.SimpleBean;
import org.limp.basework.SimpleBean4Table;
import org.limp.basework.SimpleBeanMachining;
import org.limp.basework.Table;
import org.limp.basework.impl.CommonBean4HSQ;
import org.limp.basework.impl.SimpleBean4TableImpl;
import org.limp.basework.tools.BaseworkUtil;
import org.limp.mine.DataTrimmerI;
import org.limp.mine.DateTrimmer;
import org.limp.mine.EncryptTool;
import org.limp.mine.RandomUtil;
import org.limp.mine.StringTool;

import bcxd.firstline.user.manager.AppUserManager;

/**
 * 核心企业供应商名录数据加工
 * 
 * @author mido
 * 
 */
public class FocalCompanySupplierMachining implements SimpleBeanMachining {
	/**
	 * 
	 */
	public SimpleBean machining(SimpleBean arg0, Table arg1,
			CommonBean4HSQ arg2, Connection arg3, int arg4)
			throws MachiningException {
		// TODO Auto-generated method stub
		if (arg4 == -4) {
			String pkid = StringTool
					.checkString(arg0.getResource().get("pkid"));

			String sql = "SELECT * FROM BCXD_FCSUPPLIER WHERE PK_ID = ?";
			DataTrimmerI trimmer = new DataTrimmerI(arg3);
			Map temp = trimmer.searchSingleData(sql, pkid);

			// 新建供货商用户信息
			if (temp == null) {
				BaseworkUtil util = new BaseworkUtil();
				SimpleBean4Table bxcdUserInfo = new SimpleBean4TableImpl(
						"BCXD_USER");
				bxcdUserInfo.getResource().put("uname",
						arg0.getResource().get("supname"));
				bxcdUserInfo.getResource().put("logid",
						RandomUtil.getRandomString(10));
				bxcdUserInfo.getResource().put("pwd",
						EncryptTool.encryptString("000000"));
				bxcdUserInfo.getResource().put("utype",
						arg0.getResource().get("suptype"));
				bxcdUserInfo.getResource().put("ustatus", "0");
				bxcdUserInfo.getResource()
						.put("udate", DateTrimmer.getYMDHMS());

				SimpleBean4Table supplierApplyInfo = new SimpleBean4TableImpl(
						"BCXD_FCAPPLY");
				supplierApplyInfo.getResource()
						.put("fctype", "companySupplier");
				supplierApplyInfo.getResource().put("fcname",
						arg0.getResource().get("supname"));
				supplierApplyInfo.getResource().put("fcregnumber",
						arg0.getResource().get("supregnumber"));
				supplierApplyInfo.getResource().put("fclp",
						arg0.getResource().get("suplealperson"));
				supplierApplyInfo.getResource().put("fcpermitid",
						arg0.getResource().get("supidtype"));
				supplierApplyInfo.getResource().put("fcpermitname",
						arg0.getResource().get("supidtypename"));
				supplierApplyInfo.getResource().put("fcpermitnumber",
						arg0.getResource().get("supidnumber"));
				supplierApplyInfo.getResource().put("fcphone",
						arg0.getResource().get("supphone"));
				supplierApplyInfo.getResource().put("fcauditstatus", "0");

				try {
					Map supplierApplyMap = util.manualSave1(supplierApplyInfo,
							arg3);
					String newId = (String) supplierApplyMap.get("PK_ID");

					bxcdUserInfo.getResource().put("utypeid", newId);
					Map supplierUserMap = util.manualSave1(bxcdUserInfo, arg3);

					// put for SUP_ID(BCXD_FCAPPLY)
					arg0.getResource().put("supid", newId);
				} catch (Exception ex) {
					throw new MachiningException("create supplier user error.",
							ex);
				}
			} else {
				//供应商名录信息编辑
				String supplierUserId = (String) temp.get("SUP_ID");

				AppUserManager manager = new AppUserManager(arg3);

				IUser supplierUser = manager.getBcxdUserById(supplierUserId);

				if (supplierUser == null) {
					throw new MachiningException("supplier user("
							+ supplierUserId + ") losed.");
				}

				if (supplierUser.iflag() == 0) {
					BaseworkUtil util = new BaseworkUtil();
					SimpleBean4Table bxcdUserInfo = new SimpleBean4TableImpl(
							"BCXD_USER");
					bxcdUserInfo.getResource().put("uid", supplierUserId);
					bxcdUserInfo.getResource().put("uname",
							arg0.getResource().get("supname"));
					bxcdUserInfo.getResource().put("udate",
							DateTrimmer.getYMDHMS());

					SimpleBean4Table supplierApplyInfo = new SimpleBean4TableImpl(
							"BCXD_FCAPPLY");
					supplierApplyInfo.getResource().put("pkid",
							bxcdUserInfo.getResource().get("PK_ID"));
					supplierApplyInfo.getResource().put("fcname",
							arg0.getResource().get("supname"));
					supplierApplyInfo.getResource().put("fcregnumber",
							arg0.getResource().get("supregnumber"));
					supplierApplyInfo.getResource().put("fclp",
							arg0.getResource().get("suplealperson"));
					supplierApplyInfo.getResource().put("fcpermitid",
							arg0.getResource().get("supidtype"));
					supplierApplyInfo.getResource().put("fcpermitname",
							arg0.getResource().get("supidtypename"));
					supplierApplyInfo.getResource().put("fcpermitnumber",
							arg0.getResource().get("supidnumber"));
					supplierApplyInfo.getResource().put("fcphone",
							arg0.getResource().get("supphone"));

					try {
						//更新供应商对应的注册用户表信息
						util.manualUpdate1(bxcdUserInfo, arg3);
						//更新供应商入驻申请表信息
						util.manualUpdate1(supplierApplyInfo, arg3);
					} catch (Exception ex) {
						throw new MachiningException(
								"update supplier user error.", ex);
					}
				}
			}
		}
		return null;
	}
}
